package cn.gok.exercise1.base.web;

import cn.gok.exercise1.base.service.BaseService;
import cn.gok.exercise1.dao.StudentMapper;
import cn.gok.exercise1.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

public class BaseController<S extends BaseService<Student, StudentMapper>, S1> {
    public abstract class BaseController<S extends BaseService,T> {
        @Autowired
        S service;

        public abstract String getPerfix();

        @RequestMapping
        public String list(ModelMap map){
            map.put("entityList",service.findList());
            return getPerfix()+"/list";
        }

        @GetMapping("add")
        public String add(){
            return getPerfix()+"/add";
        }


        @PostMapping("add")
        public String add(T entity){
            service.save(entity);
            return "redirect:"+getPerfix();
        }

        @GetMapping("edit/{id}")
        public String edit(@PathVariable("id") Integer id, ModelMap map){
            map.put("entity",service.findById(id));
            return getPerfix()+"/edit";
        }


        @PostMapping("edit")
        public String edit(T entity){
            service.update(entity);
            return "redirect:"+getPerfix();
        }

        @GetMapping("delete/{id}")
        public String delete(@PathVariable("id") Integer id){
            service.delete(id);
            return "redirect:"+getPerfix();
        }
    }
}
